package src.main.java.org.netWorkProgram.UDP.demo1;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/*
* 实现UDP通信-一发一收
* */
public class Client {
    public static void main(String[] args) throws Exception {
        //1.创建客户端对象
        DatagramSocket socket = new DatagramSocket();
        //2.创建数据包对象封装要发出去的数据
        //  public DatagramPacket(byte buf[], int length,InetAddress address, int port)
        /*
        * buf[] 封装要发出去的数据
        * length:发送出去的字节大小
        * address:服务端IP地址
        * port:服务端端口
        * */
        byte[] bytes = "我是快乐的客户端，我爱你abc".getBytes();
        DatagramPacket packet = new DatagramPacket(bytes,bytes.length,
                InetAddress.getLocalHost(),6666);
        //3.开始发送
        socket.send(packet);
        System.out.println("客户端数据发送完毕");
        socket.close();//释放资源
    }
}
